define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, CustomqqAdd, GoDialog, GoPage, $stateParams, $window, GoPicture) {
		$scope.id = $stateParams.id;
		$scope.detail = new Object();
		$scope.input = new Object();
		$scope.input.image = new Array();
		$scope.detail.type = 1; //默认是qq客服
		$scope.detail.value = "";
		$scope.detail.selectvalue  = {id:0,name:"请选择分类"};
		$scope.detail.name = "";
		//动态设置高度
		var all = $window.innerHeight;
		//计算滚动条
		$scope.myObj = {
			"height": all - 44 + "px"
		};
		$scope.$on('$ionicView.afterEnter', function () {
			$scope.init_data();
		}, false);
		$scope.init_data = function () {
			if ($scope.id != '')
				CustomqqAdd.detail($scope.id, function (data) {
					$scope.detail = data;
					if (data.type == 3) {
						$scope.input.image = new Object();
						$scope.input.image.image = data.image_url;
						$scope.detail.value = data.image_id;
					}
					$scope.detail.type = data.type;
					$scope.selectKefu(data.type);
				})
			CustomqqAdd.sotremember(function (data) {
				$scope.member = data.smember;
			})
		}
		$scope.doSelectType = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.member,$scope.detail.selectvalue,function(data){
				$scope.detail.value = data.member_id;
				$scope.detail.selectvalue  =  data;
			});
		}
		$scope.ondelete = function (index) {
			$scope.input.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function () {
			document.getElementById("upfilebutton").click();
		}
		$scope.addStoreImg = function () {
			GoPicture.upload(function (res) {
				if (!res.success) {
					GoDialog.toast(res.msg);
					return;
				}
				var r = res.result;
				$scope.input.image = new Object();
				$scope.detail.value = r.image_id;
				$scope.input.image.image = r.image_src;
				GoDialog.toast("添加成功！");
			});
		}
		//图片上传
		$scope.onupload = function (val) {
			GoPicture.upload(function (res) {
				if (!res.success) {
					GoDialog.toast(res.msg);
					return;
				}

				var r = res.result;
				var image_id = r.image_id;
				var image_src = r.image_src;
				// GoDialog.toast(r.image_id);

				$scope.data.avatar_id = image_id;
				$scope.data.avatar = image_src;
			});
			var fileList = val.files;
			for (var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function (e) {
					//图片进行压缩处理
					GoPicture.compress(file, e.target.result, 50, function (base64) {
						$scope.$apply(function () {
							var i = $scope.input.image.length;
							$scope.input.image[0] = {
								"image": base64
							}
							$scope.detail.value = base64;
							val.value = "";
						});
					});
				}
			}
		};
		//选择客服类型
		$scope.selectKefu = function (index) {
			//			console.log(index);
			if (index == 1) {
				document.getElementById("phone").checked = "";
				document.getElementById("QQ").checked = true;
				document.getElementById("wechat").checked = "";
				$scope.detail.value = "";
				$scope.detail.type = document.getElementById("QQ").value;
			} else if (index == 2) {
				document.getElementById("QQ").checked = "";
				document.getElementById("phone").checked = true;
				document.getElementById("wechat").checked = "";
				$scope.detail.value = "";
				$scope.detail.type = document.getElementById("phone").value;
			}
			else {
				document.getElementById("QQ").checked = "";
				document.getElementById("phone").checked = "";
				document.getElementById("wechat").checked = true;
				$scope.detail.value = "";
				$scope.detail.type = document.getElementById("wechat").value;
			}
		}

		//提交数据
		$scope.submit = function () {
			if (!$scope.detail.type) {
				GoDialog.toast("请选择客服类型！");
				return;
			} else if (!$scope.detail.name) {
				GoDialog.toast("请输入客服名称！");
				return;
			}
			else if (!$scope.detail.value) {
				GoDialog.toast("请输入客服账号\二维码图片！");
				return;
			}

			CustomqqAdd.edit($scope.id, $scope.detail.type, $scope.detail.value, $scope.detail.name, function (data) {
				if (data.success) {
					GoDialog.toast("提交成功！");
					GoPage.go_states("customqq"); //页面跳转
				} else {
					GoDialog.toast(data.msg);
				}
			});
		}

	}

	controller.$inject = ['$scope', 'CustomqqAdd', 'GoDialog', 'GoPage', '$stateParams', '$window', 'GoPicture'];

	return app.registerController("customqqAddctrl", controller);
});